import { Entity, Vector3 } from "@minecraft/server";
import { GameContext } from "@sapi-game/gameContext";
import { PlayerGroupSet } from "@sapi-game/gamePlayer/groupSet";
import { PlayerGroup } from "@sapi-game/gamePlayer/playerGroup";
import { CvSMap, CvSMaps } from "./map";
import { CvSPlayer } from "./module";

export interface CvSConfig {
    map: CvSMap;
    /**命数 */
    lives?: number;
    /**鸡的数量 */
    chickenNumber?: number;
    /**箱子刷新间隔 */
    chestRefreshInterval?: number;
    /**铁锭刷新间隔 */
    ironRefreshInterval?: number;
}

const defaultConfig: Required<CvSConfig> = {
    map: CvSMaps.classic,
    lives: 6,
    chickenNumber: 150,
    chestRefreshInterval: 60,
    ironRefreshInterval: 30,
};

export class CvSContext extends GameContext {
    readonly chicken: PlayerGroup<CvSPlayer>;
    readonly shepherd: PlayerGroup<CvSPlayer>;
    readonly groupSet: PlayerGroupSet<CvSPlayer>;
    readonly playerChickens: CvSPlayerChicken[] = [];
    readonly config: Required<CvSConfig>;

    winner: "chicken" | "shepherd" | null = null;
    lives: number;

    get map() {
        return this.config.map;
    }

    constructor(
        chicken: PlayerGroup<CvSPlayer>,
        shepherd: PlayerGroup<CvSPlayer>,
        config: CvSConfig
    ) {
        super();
        this.chicken = chicken;
        this.shepherd = shepherd;
        this.config = { ...defaultConfig, ...config };
        this.groupSet = new PlayerGroupSet([chicken, shepherd]);
        this.lives = this.config.lives;
    }
}

export class CvSPlayerChicken {
    private readonly _entity: Entity;
    readonly player: CvSPlayer;

    constructor(entity: Entity, player: CvSPlayer) {
        this._entity = entity;
        this.player = player;
        this;
    }

    get entity() {
        if (this._entity.isValid) {
            return this._entity;
        }
    }
}
